package at.fhooe.mc.master.clc.server.dataaccess.dao.objectify;

import java.util.Date;
import java.util.List;
import java.util.Map;

import at.fhooe.mc.master.clc.server.dataaccess.ErrorUtil;
import at.fhooe.mc.master.clc.server.dataaccess.dao.IMetricsDao;
import at.fhooe.mc.master.clc.shared.domain.FrameworkID;
import at.fhooe.mc.master.clc.shared.domain.metrics.IMetric;
import at.fhooe.mc.master.clc.shared.domain.metrics.Metric;
import at.fhooe.mc.master.clc.shared.domain.metrics.MetricKey;

import static at.fhooe.mc.master.clc.server.dataaccess.dao.objectify.OfyService.ofy;

public class MetricsObjectifyDao implements IMetricsDao
{

	@Override
	public List<? extends IMetric> getMetrics(
			Date startDate, Date endDate,
			FrameworkID filterId,
			MetricKey filterMetricKey)
	{
		if (filterId != null && filterMetricKey != null)
		{
			return ofy().load().type(Metric.class)
					.filter("timeStamp >=", startDate)
					.filter("timeStamp <=", endDate)
					.filter("frameworkID =", filterId)
					.filter("key =", filterMetricKey).order("timeStamp").list();
		}
		
		if (filterId != null)
		{
			return ofy().load().type(Metric.class)
					.filter("timeStamp >=", startDate)
					.filter("timeStamp <=", endDate)
					.filter("frameworkID =", filterId).order("timeStamp").list();
		}
		
		if (filterMetricKey != null)
		{
			return ofy().load().type(Metric.class)
					.filter("timeStamp >=", startDate)
					.filter("timeStamp <=", endDate)
					.filter("key =", filterMetricKey).order("timeStamp").list();
		}
		
		
		return ofy().load().type(Metric.class)
				.filter("timeStamp >=", startDate)
				.filter("timeStamp <=", endDate).order("timeStamp").list();
	}

	@Override
	public int saveMetric(IMetric metric)
	{
//		System.out.println("Save Metrics...");
		ofy().save().entity(metric).now();
		return 0;
	}

	@Override
	public int deleteMetric(IMetric metric)
	{
		ofy().delete().entity(metric).now();
		return 0;
	}

	@Override
	public int deleteOldestMetric()
	{
		return ErrorUtil.NOT_IMPLEMENTED_ERROR;
	}

}
